package com.Servlet;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@WebServlet("/pay")
public class Pay extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
        //签名方式：SIGN_TYPE = "RSA2"
        //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址:NOTIFY_URL = "http://fafa-xxxx.natapp1.cc/pay/returnUrl"
        //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址:RETURN_URL = "成功返回项目地址"
        //实例化客户端,填入所需参数
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do", "2021000118671077", "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQChUQmrJhqybeP57Tv95vT8gpaIVRC8mhd+/BpXJt85iKhxuvDpW9/gEadJ/UUyKMUGcUYUW6ZOvQ5QOs87U+yIuykuzDhVtcgXRT0mUSz6eahPF6ml5wOCGARCGSnnU3Ph7Ygw7oXsJUPZqIPOauLvKa1Y1HHHfEUaNxPS5I7v865sc+kW08OtKZ3YzvbuDexhH67XSfDlizOrYk6CbpMMGts+uOhU1vT0A8LA/Sk9mnk0l53JOXFsuKWU95skI1oeJLo4752JJz23PWSF2Su/H+LAxfS6eUocehGIjt37phnxMIJayXnIjQXL60Wk/qMFsbPLD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0keDfVsKd2CSFMoC+l6uWTKYsKBN5m45L7Sc4mwHj7EaCxRE+Pkx08lEeJVPWvDzBZNnpZhsM4kKgjpiKnAubZjspGrfNoAjkarA3IVucOhjwW2qL5WAWiFmaw45AsvYW3Oq/t3Rx+dlxX4XRB1OjXtFFJ94eZtSk3Y=", "JSON", "UTF-8", "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoVEJqyYasm3j+e07/eb0/IKWiFUQvJoXfvwaVybfOYiocbrw6Vvf4BGnSf1FMijFBnFGFFumTr0OUDrPO1PsiLspLsw4VbXIF0U9JlEs+nmoTxeppecDghgEQhkp51Nz4e2IMO6F7CVD2aiDzmri7ymtWNRxx3xFGjcT0uSO7/OubHPpFtPDrSmd2M727g3sYR+u10nw5Yszq2JOgm6TDBrbPrjoVNb09APCwP0pPZp5NJedyTlxbLillPebJCNaHiS6OO+diSc9tz1khdkrvx/iwMX0unlKHHoRiI7d+6YZ8TCCWsl5yI0Fy+tFpP6jBbGzyw+shAz1lpNiW3PhGQIDAQAB", "RSA2");
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl("http://localhost:8080/Champions/checkout.jsp");
        request.setNotifyUrl("http://127.0.0.1：8081/returnUrl");
        //根据订单编号,查询订单相关信息
//        Order order = IPayService.selectById(orderId);
//        Integer orderId = Integer.valueOf(req.getParameter("orderId"));
        SimpleDateFormat format = new SimpleDateFormat("YYYYMMddHHmmss");
        String format2 = format.format(new Date());
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {
            //有可能是负数
            hashCodeV = -hashCodeV;
        }
        String orderId = "pk" + format2 + String.format("%012d", hashCodeV);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = orderId.toString();
        //付款金额，必填
        String total_amount = req.getParameter("goods_price");
        //订单名称，必填
        String subject = req.getParameter("goods_name");
        //商品描述，可空
        String body = "";
        request.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write(form);// 直接将完整的表单html输出到页面
        resp.getWriter().flush();
        resp.getWriter().close();
    }
}
